#!/usr/bin/env python

# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from google.appengine.ext import webapp
from django.utils import simplejson as json
import logging

from lib import authz
from lib.loggable import loggable

import model

__author__ = "Maciej Machulak"
__maintainer__ = "Maciej Machulak"
__email__ = "mmachulak@google.com"

__copyright__ = "Copyright 2011, Google Inc."
__license__ = "Apache License 2.0"
__version__ = "0.1"
__status__ = "Prototype"

class StreetAddressHandler(webapp.RequestHandler):

    @authz.api_authz_required('streetaddress_read')
    @loggable('street address handler')
    def get(self, path = None):
           
      if path is None or len(path) == 0:
      
        user = self.user

        logging.debug('showing all addresses for a user %s ' % user.username)
        
        response = {}
        address_list = []
        
        for address in user.streetaddress_list:
          
          address_info = {}
          address_info['id'] = address.id
          address_info['type'] = address.type
          address_info['desc'] = address.desc
          address_info['provider'] = address.application.name
          address_list.append(address_info)
  
        response['address_list'] = address_list
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(json.dumps(response))  
      
      else:
        id = path
        logging.info('showing a single address with id: %s ' % id)
        
        address = model.StreetAddress.all().filter('id = ', id).get()
        if address is None:
          logging.error('address not found')
          self.response.set_status(404)
          return
        else:
          address_info = {}
          address_info['id'] = address.id
          address_info['type'] = address.type
          address_info['desc'] = address.desc
          address_info['provider'] = address.application.name
          self.response.headers['Content-Type'] = 'application/json'
          self.response.out.write(json.dumps(address_info))  

    @authz.api_authz_required('streetaddress_write')
    @loggable('add street address')
    def post(self, path = None):
      
      user = self.user
      application = self.application
      
      id = self.request.get('id')
      type = self.request.get('type')
      desc = self.request.get('desc')    
      
      logging.debug('obtained new street address information')
      logging.debug('id = %s' % id)
      logging.debug('type = %s ' % type)
      logging.debug('desc = %s ' % desc)
      
      logging.debug('creating a street address object')
      street_address = model.StreetAddress(
              id = id,
              type = type,
              desc = desc,
              user = user,
              application = application)
      street_address.put()
      logging.debug('street address stored within the db')
      
      self.response.set_status(201)
      return
    
    @authz.api_authz_required('streetaddress_write')  
    @loggable('delete street address')
    def delete(self, path):
  
      id = path
   
      logging.debug('retrieving street address from db with id: %s' % id)
      street_address = model.StreetAddress.all().filter('id = ', id).get()
      if street_address is None:
        logging.debug('street address not found - 404')
        self.response.set_status(404)
        return
      if street_address.application.client_id != self.application.client_id:
        logging.debug('street address found - but associated with a different client')
        self.response.set_status(403)
        return
      logging.debug('street address found - deleting')
      street_address.delete()
      logging.debug('address deleted from db - returning 204')
      self.response.set_status(204)    


